//*****************************************************************************
// JFab (http://code.google.com/p/jfab)
// Copyright (c) 2011-2012 JFab.org
// Admin jfab@jeffreythompson.net
//
// See the file "LICENSE.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//*****************************************************************************

package org.jfab.swingui.editpane;

import javax.swing.JTextArea;

import org.jfab.core.model.CGConstructor;

/**
 * Provides an edit pane for a code generator constructor.
 */
public final class CGConstructorEditPane extends
        CGObjectEditPane<CGConstructor>
{
    /** Serial version UID. */
    private static final long serialVersionUID = 1L;

    /** Method body lines widget. */
    private JTextArea _methodBodyLinesUI;

    /** Throws types widget. */
    private JTextArea _throwsTypesUI;

    /**
     * Construct this object with the given parameter.
     * 
     * @param cgConstructor Code generator constructor.
     */
    public CGConstructorEditPane(final CGConstructor cgConstructor)
    {
        super(cgConstructor);
    }

    @Override
    public final void applyChanges()
    {
        super.applyChanges();

        final CGConstructor subject = getSubjectObject();

        subject.getThrowsTypes().clear();
        subject.getThrowsTypes().addAll(
                getCgTypeListAdapter().unmarshal(_throwsTypesUI.getText()));

        subject.getMethodBodyLines().clear();
        subject.getMethodBodyLines().addAll(
                getStringListAdapter().unmarshal(_methodBodyLinesUI.getText()));
    }

    @Override
    protected final void createWidgets()
    {
        super.createWidgets();

        _methodBodyLinesUI = createTextArea();
        _throwsTypesUI = createTextArea();
    }

    @Override
    protected final void fillPanel()
    {
        super.fillPanel();

        bindAndAdd("throwsTypes", _throwsTypesUI, getCgTypeAdapter());
        bindAndAdd("methodBodyLines", _methodBodyLinesUI,
                getStringListAdapter());
    }

    @Override
    protected final void fillWidgets()
    {
        super.fillWidgets();

        final CGConstructor subject = getSubjectObject();

        _throwsTypesUI.setText(getCgTypeListAdapter().marshal(
                subject.getThrowsTypes()));
        _methodBodyLinesUI.setText(getStringListAdapter().marshal(
                subject.getMethodBodyLines()));
    }
}
